package com.example.yixueguan.bean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.example.yixueguan.database.MyDBHelper;

public class StudentsDAO {
    private Context context;
    private MyDBHelper dbHelper;
    private SQLiteDatabase db;

    //构造函数
    public StudentsDAO(Context context) {
        this.context = context;
    }

    //打开数据库
    public void open() throws SQLiteException {
        dbHelper = new MyDBHelper(context);
        try {
            db = dbHelper.getWritableDatabase();
        } catch (SQLiteException ex) {
            db = dbHelper.getReadableDatabase();
        }
    }

    //关闭数据库
    public void close() {
        if (db != null) {
            db.close();
            db = null;
        }
    }

    //添加学生信息
    public long addStudents(Student o) {
        // 创建ContentValues对象
        ContentValues values = new ContentValues();
        // 向该对象中插入键值对
        values.put("studentid", o.studentid);
        values.put("studentname", o.studentname);
        values.put("majoy", o.majoy);
        values.put("studentclass", o.studentclass);

        // 调用insert()方法将数据插入到数据库当中
        return db.insert("tb_Students", null, values);
    }

    //删除指定学生信息
    public int deletStudents(Student o) {
        return db.delete("tb_Students", "studentid=?", new String[]{String.valueOf(o.studentid)});
    }

    //修改指定学生信息
    public int updateStudents(Student o) {
        ContentValues value = new ContentValues();
        value.put("studentname", o.studentname);
        value.put("majoy", o.majoy);
        value.put("studentclass", o.studentclass);
        return db.update("tb_Students", value, "studentid=?", new String[]{String.valueOf(o.studentid)});
    }

    //根据学生学号查找信息
    public Student getStudents(String studentid) {
        //查询学生
        Cursor cursor = db.query("tb_Students", null, "studentid=?", new String[]{studentid}, null, null, null);
        Student o = new Student();
        while (cursor.moveToNext()) {
            o.studentid = cursor.getString(cursor.getColumnIndex("studentid"));
            o.studentname = cursor.getString(cursor.getColumnIndex("studentname"));
            o.majoy = cursor.getString(cursor.getColumnIndex("majoy"));
            o.studentclass = cursor.getString(cursor.getColumnIndex("studentclass"));
        }
        cursor.close();
        return o;
    }

    //查看所有学生信息
    public ArrayList<Map<String, Object>> getAllstudents() {
        ArrayList<Map<String, Object>> listStudents = new ArrayList<Map<String, Object>>();
        Cursor cursor = db.query("tb_Students", null, null, null, null, null,null);
        if (cursor.moveToFirst()) {
            do {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("studentid", cursor.getString(cursor.getColumnIndex("studentid")));
                map.put("studentname", cursor.getString(cursor.getColumnIndex("studentname")));
                map.put("majoy", cursor.getString(cursor.getColumnIndex("majoy")));
                map.put("studentclass", cursor.getString(cursor.getColumnIndex("studentclass")));
                listStudents.add(map);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return listStudents;
    }
}